package com.sbhotel.chotellauncher.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.callback.HttpCallBackListener;
import com.sbhotel.chotellauncher.constant.Constants;
import com.sbhotel.chotellauncher.entity.BaseResponseBean;
import com.sbhotel.chotellauncher.entity.GroupInfoBean;
import com.sbhotel.chotellauncher.util.HttpUtil;
import com.sbhotel.chotellauncher.util.SPUtil;

public class GroupActivity extends BaseActivity {

    EditText etGroup;
    TextView tvGroupName;
    View joinBtn;
    String groupId = "";
    String groupName = "";

    @Override
    public void initView() {
        etGroup = findViewById(R.id.et_group);
        etGroup.addTextChangedListener(watcher);

        tvGroupName = findViewById(R.id.tv_group_name);
        joinBtn = findViewById(R.id.btn_join_group);
        joinBtn.setVisibility(View.GONE);
        joinBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bindGroup();
            }
        });
    }

    @Override
    public void initData() {
        initTitleBar("群组设置", "");
    }

    @Override
    public int initLayout() {
        return R.layout.activity_group;
    }

    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String groupId = editable.toString();
            if (groupId.length() >= 6) {
                getGroupInfo();
            }
        }
    };

    private void getGroupInfo() {
        String deviceUn = (String) SPUtil.get(this, Constants.SP_KEY_DEVICE_UN_CODE, "");
        if (TextUtils.isEmpty(deviceUn)) return;
        String uniMark = etGroup.getText().toString();
        if (TextUtils.isEmpty(uniMark) || uniMark.length() < 6) return;
        String param = "service=getGroupInfo&version=" + Constants.INTEREFACT_VERSION
                + "&device_un=" + deviceUn
                + "&uni_mark=" + uniMark;
        HttpUtil.doPostAsyn(Constants.MAIN_URL, param, new HttpCallBackListener() {
            @Override
            public void onFinish(String response) {
                GroupInfoBean bean = new Gson().fromJson(response, GroupInfoBean.class);
                if (bean.getCode() == 0) {
                    groupId = bean.getData().getGid();
                    groupName = bean.getData().getName();
                    Message msg = Message.obtain();
                    msg.what = 0;
                    msg.obj = bean.getData().getName();
                    mHandler.sendMessage(msg);
                } else {
                    mHandler.sendEmptyMessage(1);
                }
            }

            @Override
            public void onError(Exception e) {

            }
        });
    }

    private void bindGroup() {
        String deviceUn = (String) SPUtil.get(this, Constants.SP_KEY_DEVICE_UN_CODE, "");
        if (TextUtils.isEmpty(deviceUn)) return;
        String param = "service=bindGroup&version=" + Constants.INTEREFACT_VERSION
                + "&device_un=" + deviceUn
                + "&group_id=" + groupId;
        HttpUtil.doPostAsyn(Constants.MAIN_URL, param, new HttpCallBackListener() {
            @Override
            public void onFinish(String response) {
                final BaseResponseBean baseResponseBean = new Gson().fromJson(response, BaseResponseBean.class);
                if (baseResponseBean.getCode() == 0) {
                    Intent intent = new Intent();
                    intent.putExtra("groupName", groupName);
                    setResult(2, intent);
                    SPUtil.put(GroupActivity.this, Constants.SP_KEY_GROUP_NAME, groupName);
                    GroupActivity.this.finish();
                } else {
                    //mHandler.sendEmptyMessage(1);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(GroupActivity.this, "错误码：" + baseResponseBean.getCode() + " " + baseResponseBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }

            @Override
            public void onError(Exception e) {

            }
        });
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0: //正确获取群组信息
                    String groupName = (String) msg.obj;
                    tvGroupName.setVisibility(View.VISIBLE);
                    tvGroupName.setText(groupName);
                    joinBtn.setVisibility(View.VISIBLE);
                    break;
                case 1://获取群组信息错误
                    joinBtn.setVisibility(View.GONE);
                    tvGroupName.setVisibility(View.VISIBLE);
                    tvGroupName.setText("激活码错误，请重新输入");
                    break;
                default:
                    break;
            }
        }
    };
}
